﻿using Device;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Input;
using WpfApp.Common;
using WpfApp.Extensions;
using WpfApp.Models;
using WpfApp.UserControls;

namespace WpfApp.ViewModels
{
    public class UdpAnchorViewModel : BindableBase
    {
        private ObservableCollection<string> _LocalIpList;
        public ObservableCollection<string> LocalIpList
        {
            get => _LocalIpList;
            set => SetProperty(ref _LocalIpList, value);
        }
        private string _SelectedIp;
        public string SelectedIp
        {
            get => _SelectedIp;
            set => SetProperty(ref _SelectedIp, value);
        }
        private ObservableCollection<Anchor> _Anchors;
        public ObservableCollection<Anchor> Anchors
        {
            get => _Anchors;
            set => SetProperty(ref _Anchors, value);
        }
        private Anchor _SelectedAnchor;
        public Anchor SelectedAnchor
        {
            get => _SelectedAnchor;
            set => SetProperty(ref _SelectedAnchor, value);
        }
        private ObservableCollection<LogInfo> _LogInfos;
        public ObservableCollection<LogInfo> LogInfos
        {
            get => _LogInfos;
            set => SetProperty(ref _LogInfos, value);
        }
        private bool _IsIpChangedEnabled = true;
        public bool IsIpChangedEnabled
        {
            get => _IsIpChangedEnabled;
            set => SetProperty(ref _IsIpChangedEnabled, value);
        }
        private readonly IUdpService UdpService;

        private readonly IDialogHostService dialogHostService;
        public ICommand RefreshCommand { get; private set; }
        public ICommand ListenCommand { get; private set; }
        public ICommand DistanceConfigCommand { get; private set; }
        public ICommand AddTagIdCommand { get; private set; }

        public ICommand AnchorConfigCommand { get; private set; }
        public ICommand FactoryConfigCommand { get; private set; }
        
        public ICommand ClearTagIdCommand { get; private set; }
        
        public UdpAnchorViewModel(IUdpService _udpService, IDialogHostService _dialogHostService)
        {
            DistanceConfigCommand = new DelegateCommand(DistanceConfig);
            AddTagIdCommand = new DelegateCommand(AddTagId);
            ClearTagIdCommand = new DelegateCommand(ClearTagId);
            RefreshCommand = new DelegateCommand(RefreshAsync);
            ListenCommand = new DelegateCommand<bool?>(Listen);
            LogInfos = new ObservableCollection<LogInfo>();
            Anchors = new ObservableCollection<Anchor>();
            LocalIpList = new ObservableCollection<string>(NetworkHelper.GetLocalIp4List());
            UdpService = _udpService;
            UdpService.RecvMsgEvent += UdpService_RecvMsgEvent;
            dialogHostService = _dialogHostService;
        }

        private void ClearTagId()
        {
            UdpService.ClearTagId(SelectedAnchor);
        }

        private void AddTagId()
        {
            UdpService.AddTagId(SelectedAnchor,"1111");
        }

        private void DistanceConfig()
        {
            UdpService.ConfigDistance(SelectedAnchor);
        }

        private async void UdpService_RecvMsgEvent(MsgEventArgs e)
        {
            switch (e.MsgType)
            {
                case MsgType.Log:
                    await App.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        LogInfos.Add(new LogInfo { DateTime = DateTime.Now, Message = $"{e.msg}" });
                    }), null);
                    break;
                case MsgType.AnchorInfo:
                    var anchor = JsonSerializer.Deserialize<Anchor>(e.msg);
                    await App.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        if (!Anchors.Any(a => a.AnchorId == anchor.AnchorId))
                        {
                            Anchors.Add(anchor);
                        }
                    }), null);
                    break;
                default:
                    break;
            }
        }

        private void Listen(bool? flag)
        {
            if (flag == true && !string.IsNullOrEmpty(SelectedIp))
            {
                UdpService.SettingIpPort(SelectedIp);
                UdpService.Sart();
                LogInfos.Add(new LogInfo { DateTime = DateTime.Now, Message = $"开启UDP监听{SelectedIp}" });
            }
            else if (flag == false)
            {
                UdpService.Stop();
                LogInfos.Add(new LogInfo { DateTime = DateTime.Now, Message = $"关闭UDP监听{SelectedIp}" });
            }
        }

        private async void RefreshAsync()
        {
            Anchors.Clear();
            await UdpService.Refresh();
            //var dialogResult = await dialogHostService.Question("提示", "刷新成功");
            //if (dialogResult.Result != Prism.Services.Dialogs.ButtonResult.OK)
            //    return;
        }
    }
}
